﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Configuration;

/// <summary>
/// Bank Web Service for Project Monopoly
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Banka : System.Web.Services.WebService
{

    public Banka()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public bool insertNewPlayer(int playerID, string token, int balance)
    {
        try
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["banka"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);

            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("INSERT INTO Player VALUES (@PlayerID, @Token, @Balance)", conn);
                cmd.Parameters.AddWithValue("@PlayerID", playerID);
                cmd.Parameters.AddWithValue("@Token", token);
                cmd.Parameters.AddWithValue("@Balance", balance);
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }

    [WebMethod]
    public bool depositMoney(int playerID, int amount)
    {
        try
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["banka"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);

            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Balance=Balance+@Amount WHERE ID = @PlayerID", conn);
                cmd.Parameters.AddWithValue("@Amount", amount);
                cmd.Parameters.AddWithValue("@PlayerID", playerID);
                if (cmd.ExecuteNonQuery() == 0) throw new Exception("Wrong ID or ID with that value does not exist...");
            }
        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }
    [WebMethod]
    public bool withdrawMoney(int playerID, int amount)
    {
        try
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["banka"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(connectionString);

            using (conn)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Balance=Balance-@Amount WHERE ID = @PlayerID", conn);
                cmd.Parameters.AddWithValue("@Amount", amount);
                cmd.Parameters.AddWithValue("@PlayerID", playerID);
                if (cmd.ExecuteNonQuery() == 0) throw new Exception("Wrong ID or ID with that value does not exist...");
            }
        }
        catch (Exception err)
        {
            return false;
        }
        return true;
    }
    [WebMethod]
    public int showBalance(int playerID)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["banka"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(connectionString);
        using (conn)
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT Balance FROM Player WHERE ID=@PlayerID", conn);
            cmd.Parameters.AddWithValue("@PlayerID", playerID);
            int balance = Convert.ToInt32(cmd.ExecuteScalar());

            //If there are no rows returned Exception is thrown
            conn.Close();

            return balance;

        }
        //This code is unreachable
        return -1;
    }

}
